package Z.dailyExercise._0225;

import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class _剑指Offer06从尾到头打印链表 {
    /**
     * 借助栈
     * 加速方法：不用栈 先遍历一边计算长度 再从数组的后面依次赋值
     * @param head
     * @return
     */
    public int[] reversePrint(ListNode head) {

        if (head == null){
            return null;
        }
        Stack<Integer> stack = new Stack<>();
        //i
        //List<Integer> list = new ArrayList<>() ;
        int count =0;
        while (head != null){
            //res[count++]=head.val;
            stack.push(head.val);
            head = head.next;
        }
        int[] res = new int[stack.size()];
//        for (int i=0;i<stack.size();i++){
//            res[i] = stack.pop();
//        }
        while (!stack.isEmpty()){
            res[count++] = stack.pop();
        }
      //  return list.stream().mapToInt(Integer::valueOf).toArray();
        return res;

    }

}
